﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Shared;
using System.Drawing;
using ImageConverter;

namespace OnlineShop
{
    class MainPresenter
    {
        private IMainView _mainView;
        private IProductRepository _product;
        private ICategoryRepository _category;

        public MainPresenter(IMainView view, ICategoryRepository category, IProductRepository product)
        {
            _mainView = view;
            _product = product;
            _category = category;                  

            _mainView.CategoryChanged += _mainView_CategoryChanged;
            _mainView.RowSelected += _mainView_RowSelected;
            _mainView.Switch_clicked += _mainView_Switch;

        }
        
        private void _mainView_RowSelected()
        {
            
                int prodId = _mainView.ReturnSelectedProductId();
                SetProductInfo(_product.GetProductByProductId(prodId));            
        }

        private void _mainView_CategoryChanged()
        {
            string prodCat = _mainView.ReturnSelectedCategoryTable();
           
                SetProductsToView(_product.GetProductByCategory(prodCat));
                ClearProductInfo();   
        }

        private void _mainView_Switch()
        {
            LoginPresenter pr = new LoginPresenter(new AuthorizationForm(), Initializator.GetUserRepository());
            pr.Run();
        }

        private void SetProductInfo(Product product)
        {
            _mainView.SetName(product.Name);
            _mainView.SetPrice( product.Price.ToString());
            _mainView.ProductCategoryFromProdInfo = product.Category;
            _mainView.SetDescription(product.Description);
            if (product.Image == null)
            {
                _mainView.SetPictureSet(Image.FromFile(@"C:\Users\vbelich\Desktop\codeplex\vbelich\OnlineShop\PicturesForShop\logo.png"));
            }
            else
            {
                _mainView.SetPictureSet(ImageConvert.byteArrayToImage(product.Image));
            }
        }

        private void ClearProductInfo()
        {
            _mainView.SetName(string.Empty);
            _mainView.SetPrice(string.Empty);
            _mainView.ProductCategoryFromProdInfo = string.Empty;
            _mainView.SetDescription(string.Empty);
            _mainView.SetPictureSet(null);
        }
        

        private void SetProductCatToTable(ICollection<string> listOfProductCategories)
        {
            _mainView.SetListCategories(listOfProductCategories);
        }

        private void SetProductCategoriesToProdcbx(ICollection<string> listOfProductCategories)
        {
            _mainView.SetListCategoriesForProduct(listOfProductCategories);
        }

        private void SetProductsToView(ICollection<Product> products)
        {
            _mainView.tableProducts = products;
        }
        
        public void Run()
        {
            _mainView.SetListCategories(_category.GetAllCategories());

            this._mainView.Show();
        } 
    }
}
